import logging
import os
from fastapi import FastAPI, Depends
from application.config import PROJECT_NAME
from fastapi.middleware.cors import CORSMiddleware
from application.util.TimeUtil import now_format_date
from application.util.MysqlUtil import DATABASE_CONFIG
from application.config.DatabaseConfig import MysqlConfig
from tortoise.contrib.fastapi import register_tortoise
from application.exception import low_exception_handler
from application.exception.BasicException import BasicException
from application.config.ServerConfig import ServerConfig, CORSConfig
from application.middleware.ProcessMiddleware import ProcessMiddleware
from application.controller import CommonController, UserController, router
from application.dependency.TokenDependency import verify_token, get_current_user

# 创建日志目录（如果不存在）
os.makedirs(name=ServerConfig.log_dir, exist_ok=True)

# 配置tortoise日志记录
logging.basicConfig(level=logging.DEBUG,
                    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",  # 设置日志格式
                    filename=f"{ServerConfig.log_dir}/{now_format_date()}.tortoise.log",  # 设置日志文件名
                    filemode="w")
logger: logging.Logger = logging.getLogger("tortoise")
logger.setLevel(logging.DEBUG)

# 创建FastAPI实例
app: FastAPI = FastAPI(title=PROJECT_NAME, description=PROJECT_NAME)
# 注册Tortoise ORM
register_tortoise(app=app, config=DATABASE_CONFIG, generate_schemas=MysqlConfig.auto_create_table)

# 配置CORS跨域中间件
app.add_middleware(middleware_class=CORSMiddleware, allow_origins=CORSConfig.allow_origins, 
                   allow_methods=CORSConfig.allow_methods, allow_headers=CORSConfig.allow_headers)
# 配置请求响应中间件
app.add_middleware(middleware_class=ProcessMiddleware)

# 配置路由、添加依赖
app.include_router(router=router)  # 根路由
app.include_router(router=CommonController.router)
app.include_router(router=UserController.router)

# 配置异常
app.add_exception_handler(exc_class_or_status_code=BasicException, handler=BasicException.exception_handler)
app.add_exception_handler(exc_class_or_status_code=Exception, handler=low_exception_handler)
